#import "DingRtcPlugin.h"
#import "DingRtcViewFactory.h"
#import "DingRtcManager.h"

@interface DingRtcPlugin()

@property(nonatomic, strong) FlutterMethodChannel *channel;

@end

@implementation DingRtcPlugin
- (instancetype)initWithChannel:(FlutterMethodChannel *)channel {
    self = [super init];
    if (self) {
        _channel = channel;
    }
    return self;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"ding_rtc_engine"
            binaryMessenger:[registrar messenger]];
    
  DingRtcPlugin* instance = [[DingRtcPlugin alloc] initWithChannel:channel];
  [registrar addMethodCallDelegate:instance channel:channel];
    
    // 注册 PlatformView（视频视图）
    DingRtcViewFactory* factory = [[DingRtcViewFactory alloc] initWithMessenger:[registrar messenger]];
    
    [registrar registerViewFactory:factory withId:@"DingRtcVideoView"];
}


- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    NSString *method = call.method;
    
    if ([method isEqualToString:@"initEngine"]) {
        NSString *extra = (NSString *)call.arguments;
        [[DingRtcManager sharedInstance] initWithPlugin:self extra:extra];
        result(nil);
        
    } else if ([method isEqualToString:@"destroyEngine"]) {
        [[DingRtcManager sharedInstance] destroy];
        result(nil);
        
    } else if ([method isEqualToString:@"enterRoom"]) {
        NSDictionary *params = (NSDictionary *)call.arguments;
        [[DingRtcManager sharedInstance] enterRoomWithParams:params];
        result(nil);
        
    } else if ([method isEqualToString:@"exitRoom"]) {
        [[DingRtcManager sharedInstance] leaveRoom];
        result(nil);
        
    } else if ([method isEqualToString:@"startLocalPreview"]) {
        NSNumber *front = call.arguments[@"front"];
        NSNumber *viewId = call.arguments[@"viewId"];
        [[DingRtcManager sharedInstance] startLocalPreviewWithCamera:[front boolValue] viewId:[viewId intValue]];
        result(nil);
        
    } else if ([method isEqualToString:@"startRemoteView"]) {
        NSString *userId = call.arguments[@"userId"];
        NSNumber *viewId = call.arguments[@"viewId"];
        [[DingRtcManager sharedInstance] startRemoteViewWithUserId:userId viewId:[viewId intValue]];
        result(nil);
        
    } else if ([method isEqualToString:@"muteLocalAudio"]) {
        NSNumber *mute = (NSNumber *)call.arguments;
        [[DingRtcManager sharedInstance] muteLocalAudio:[mute boolValue]];
        result(nil);
        
    } else if ([method isEqualToString:@"enableLocalVideo"]) {
        NSNumber *enable = (NSNumber *)call.arguments;
        [[DingRtcManager sharedInstance] enableLocalVideo:[enable boolValue]];
        result(nil);
        
    } else if ([method isEqualToString:@"enableSpeakerphone"]) {
        NSNumber *enable = (NSNumber *)call.arguments;
        [[DingRtcManager sharedInstance] enableSpeakerphone:[enable boolValue]];
        result(nil);
        
    } else if ([method isEqualToString:@"switchCamera"]) {
        [[DingRtcManager sharedInstance] switchCamera];
        result(nil);
        
    } else if ([method isEqualToString:@"setLogLevel"]) {
        NSNumber *level = (NSNumber *)call.arguments;
        [[DingRtcManager sharedInstance] setLogLevel:[level intValue]];
        result(nil);
        
    }  else if ([method isEqualToString:@"snapshotVideo"]) {
        NSString *userId = call.arguments[@"userId"];
        NSString *path = call.arguments[@"path"];
        NSNumber *isScreen = call.arguments[@"isScreen"];
        result([[DingRtcManager sharedInstance] snapshotVideo:userId filePath:filePath isScreen:[isScreen boolValue]]);

    } else {
        result(FlutterMethodNotImplemented);
    }
}



- (void)dealloc {
    self.channel = nil;

}

- (void)callBackToFlutterWithMethod:(NSString *)method arguments:(id)arguments {
    if (self.channel) {
        [self.channel invokeMethod:method arguments:arguments];
    }
}

@end
